var mqtt = require('mqtt')
const cluster = require('cluster')
const os = require('os')
//console.log(os.cpus())

function getWorkNo() {
	//return -1
  if(process.env.instance_var != undefined) { //pm2模式
     return Number(process.env[process.env.instance_var])
  }else {
    return cluster.isPrimary ? 0 : process.env.workNo
  }
}

const workNo = getWorkNo()

if(workNo == 0) {
	for(let i = 10000; i <= 12000; i ++) {
		const no = String(i).padEnd(20, '0')
		const toTopic = 'deviceToServer/' + no
		const client  = mqtt.connect({
		  username : 'bill',
		  password : '123456'
		})
		
		client.on('message', function (topic, message) {
			//console.log(`message:${i}`)
			//console.log(message.toString())
		})
		
		client.on('connect', function () {
			console.log(`connect:${i}`)
		  client.subscribe(['serverReplyDevice/' + no], {qos : 2}, (error, granted) => {
			  client.publish(toTopic, JSON.stringify({cmdType : 1,  packType :0, "outletStatus":[0,1,0,2,4,0,0,2,1,3]}))
		  })
		})
		
	}
	if(cluster.isPrimary) {
		for(let i = 1; i <= 4; i ++) {
		  cluster.fork({workNo : i})
		}
	  }
}

if(workNo == 1) {
	for(let i = 12001; i <= 14000; i ++) {
		const no = String(i).padEnd(20, '0')
		const toTopic = 'deviceToServer/' + no
		const client  = mqtt.connect({
		  username : 'bill',
		  password : '123456'
		})
		
		client.on('message', function (topic, message) {
			//console.log(`message:${i}`)
			//console.log(message.toString())
		})
		
		client.on('connect', function () {
			console.log(`connect:${i}`)
		  client.subscribe(['serverReplyDevice/' + no], {qos : 2}, (error, granted) => {
			  client.publish(toTopic, JSON.stringify({cmdType : 1,  packType :0, "outletStatus":[0,1,0,2,4,0,0,2,1,3]}))
		  })
		})
		
	}
}

if(workNo == 2) {
	for(let i = 14001; i <= 16000; i ++) {
		const no = String(i).padEnd(20, '0')
		const toTopic = 'deviceToServer/' + no
		const client  = mqtt.connect({
		  username : 'bill',
		  password : '123456'
		})
		
		client.on('message', function (topic, message) {
			//console.log(`message:${i}`)
			//console.log(message.toString())
		})
		
		client.on('connect', function () {
			console.log(`connect:${i}`)
		  client.subscribe(['serverReplyDevice/' + no], {qos : 2}, (error, granted) => {
			  client.publish(toTopic, JSON.stringify({cmdType : 1,  packType :0, "outletStatus":[0,1,0,2,4,0,0,2,1,3]}))
		  })
		})
		
	}
}

if(workNo == 3) {
	for(let i = 16001; i <= 18000; i ++) {
		const no = String(i).padEnd(20, '0')
		const toTopic = 'deviceToServer/' + no
		const client  = mqtt.connect({
		  username : 'bill',
		  password : '123456'
		})
		
		client.on('message', function (topic, message) {
			//console.log(`message:${i}`)
			//console.log(message.toString())
		})
		
		client.on('connect', function () {
			console.log(`connect:${i}`)
		  client.subscribe(['serverReplyDevice/' + no], {qos : 2}, (error, granted) => {
			  client.publish(toTopic, JSON.stringify({cmdType : 1,  packType :0, "outletStatus":[0,1,0,2,4,0,0,2,1,3]}))
		  })
		})
		
	}
}
// if(workNo == 4) {
// 	for(let i = 18001; i <= 20000; i ++) {
// 		const no = String(i).padEnd(20, '0')
// 		const toTopic = 'deviceToServer/' + no
// 		const client  = mqtt.connect({
// 		  username : 'bill',
// 		  password : '123456'
// 		})
		
// 		client.on('message', function (topic, message) {
// 			//console.log(`message:${i}`)
// 			//console.log(message.toString())
// 		})
		
// 		client.on('connect', function () {
// 			console.log(`connect:${i}`)
// 		  client.subscribe(['serverReplyDevice/' + no], {qos : 2}, (error, granted) => {
// 			  client.publish(toTopic, JSON.stringify({cmdType : 1,  packType :0, "outletStatus":[0,1,0,2,4,0,0,2,1,3]}))
// 		  })
// 		})
		
// 	}
// }

